/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof_trab1;

import java.util.Random;

/**
 *
 * @author Carlos Pinheiro
 */
public class ExecutorDownloader implements Runnable
{
    private ResizeableSemaphore rs;
    
    public ExecutorDownloader (ResizeableSemaphore sem) throws InterruptedException
    {
        this.rs = sem;
        this.rs.acquire();
    }
    
    @Override
    public void run()
    {
        Random generator = new Random();
        try
        {           
            long downloading = generator.nextInt(100000) + 1;
            System.out.println("Downloader: " + this.toString() + " started...");
            
            BandwithAllocation.getInstance().addDownload(downloading);
            Thread.sleep(2000);
            BandwithAllocation.getInstance().removeDownload(downloading);
            
            System.out.println("Downloader: " + this.toString() + " finished.");
            
            this.rs.release();
        }
        catch (InterruptedException ex)
        {
        }
    }    
}
